package com.csd.dao;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.csd.helper.IConstant;
import com.csd.pojo.ChangeRecord;
import com.csd.pojo.Project;
import com.csd.pojo.Teacher;

/**
 * A data access object (DAO) providing persistence and search support for
 * Change entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .Change
 * @author MyEclipse Persistence Tools
 */

public class ChangeDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ChangeDAO.class);
	// property constants
	public static final String CONTENT = "content";
	public static final String REASON = "reason";
	public static final String OPINION = "opinion";
	public static final String RESULT = "result";
	public static final String BACKUP = "backup";

	protected void initDao() {
		// do nothing
	}

	public void save(ChangeRecord transientInstance) {
		log.debug("saving Change instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ChangeRecord persistentInstance) {
		log.debug("deleting Change instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 更新比更记录
	 * @param persistentInstance 变更记录实例
	 */
	public void update(ChangeRecord persistentInstance) {
        log.debug("updating Change instance");
        try {
            getHibernateTemplate().update(persistentInstance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }
	
	/**
	 * 查询管理员未处理的的变更
	 * @return 
	 */
	public List<ChangeRecord> listUnhandleCROfManager() {
        log.debug("find unhandle Change of manager");
        List<ChangeRecord> resultsList = null;
        try {
        	String hqlString = "from ChangeRecord as model where result = '"+IConstant.changeRecordPass
        		+"' and state = '"+IConstant.managerUnhandle+"'";
            resultsList = getHibernateTemplate().find(hqlString);
            log.debug("finding successful");
        } catch (RuntimeException re) {
            log.error("finding failed", re);
            throw re;
        }
        return resultsList;
    }
	
	/**
	 * 查询管理员驳回的变更
	 * @return 
	 */
	public List<ChangeRecord> listRejectCROfManager() {
        log.debug("reject Change of manager");
        List<ChangeRecord> resultsList = null;
        try {
        	String hqlString = "from ChangeRecord as model where result = '"+IConstant.changeRecordReject
        		+"' and state = '"+IConstant.managerHandle+"'";
            resultsList = getHibernateTemplate().find(hqlString);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
        return resultsList;
    }
	
	/**
	 * 查询管理员通过的变更
	 * @return 
	 */
	public List<ChangeRecord> listAcceptCROfManager() {
        log.debug("reject Change of manager");
        List<ChangeRecord> resultsList = null;
        try {
        	String hqlString = "from ChangeRecord as model where result = '"+IConstant.changeRecordPass
        	+"' and state = '"+IConstant.managerHandle+"'";
            resultsList = getHibernateTemplate().find(hqlString);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
        return resultsList;
    }
	public ChangeRecord findById(java.lang.Integer id) {
		log.debug("getting Change instance with id: " + id);
		try {
			ChangeRecord instance = (ChangeRecord) getHibernateTemplate().get(
					ChangeRecord.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<ChangeRecord> findByProjectID(Integer id) {
		ProjectDAO projectDAO = new ProjectDAO();
		Project project = projectDAO.findById(id);
		List<ChangeRecord> changeRecords = null;
		String hql = " from ChangeRecord as cr where cr.project=?";
		changeRecords = this.getHibernateTemplate().find(hql, project);
		return changeRecords;
	}

	public List findByExample(ChangeRecord instance) {
		log.debug("finding Change instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Change instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ChangeRecord as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByReason(Object reason) {
		return findByProperty(REASON, reason);
	}

	public List findByOpinion(Object opinion) {
		return findByProperty(OPINION, opinion);
	}

	public List findByResult(Object result) {
		return findByProperty(RESULT, result);
	}

	public List findByBackup(Object backup) {
		return findByProperty(BACKUP, backup);
	}

	public List findAll() {
		log.debug("finding all Change instances");
		try {
			String queryString = "from Change";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ChangeRecord merge(ChangeRecord detachedInstance) {
		log.debug("merging Change instance");
		try {
			ChangeRecord result = (ChangeRecord) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void updateProjectChange(ChangeRecord changeRecord) {
		this.getHibernateTemplate().saveOrUpdate(changeRecord);
	}

	public void attachDirty(ChangeRecord instance) {
		log.debug("attaching dirty Change instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ChangeRecord instance) {
		log.debug("attaching clean Change instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
    /** 查询老师需要审批的变更记录*/
	public List<ChangeRecord> listChangeRecordByTeacher(Teacher teacher) {
		List<ChangeRecord> changeRecords = null;
		String queryString = "from ChangeRecord as cr where cr.state ='"+IConstant.teacherUnhandle+"'and cr.project.teacher.id = "+teacher.getId();
		try { 
			changeRecords = this.getHibernateTemplate().find(queryString);
		} catch (Exception e) {
		    System.out.println("ChangDAO.listChangeRecordByTeacher()");
		    e.printStackTrace();
		}
		return changeRecords;
	}


	public static ChangeDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ChangeDAO) ctx.getBean("ChangeDAO");
	}
}